﻿// Copyright (c) JosephZheng. All rights reserved. See License.txt in the project root for license

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MessageBoxHelperLib;

namespace Lyra.Cache.HelperTool.SubForm
{
    public partial class CreateSchemaForm : Form
    {
        private CheckBox _cb = new CheckBox();

        public CreateSchemaForm()
        {
            InitializeComponent();
            Schema = new Dictionary<string, string>();
            View = new Dictionary<string, string>();
            _cb.Checked = true;
            _cb.Text = "Send Create to Server";
            ToolStripControlHost host = new ToolStripControlHost(_cb);
            toolStrip1.Items.Insert(2, host);
        }

        public bool SendToServer
        {
            get
            {
                return _cb.Checked;
            }
        }

        public Dictionary<string, string> Schema { get; set; }
        public Dictionary<string, string> View { get; set; }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.dgvMain.RowCount != 0)
            {
                Schema.Clear();
                View.Clear();
                foreach (DataGridViewRow row in this.dgvMain.Rows)
                {
                    if (!string.IsNullOrWhiteSpace(row.Cells[0].Value as string))
                    {
                        if (!string.IsNullOrWhiteSpace(row.Cells[1].Value as string))
                        {
                            if (!Schema.ContainsKey(((string)row.Cells[0].Value).Trim()))
                            {
                                Schema.Add(((string)row.Cells[0].Value).Trim(), ((string)row.Cells[1].Value).Trim());
                            }
                            else
                            {
                                MessageBoxHelper.ShowInfo("duplicate schema rows!");
                                return;
                            }
                        }
                        else
                        {
                            MessageBoxHelper.ShowInfo("value type unselected!");
                            return;
                        }
                    }
                }
                if (Schema.Count != 0)
                {
                    if (this.dgvMain2.Rows.Count != 0)
                    {
                        foreach (DataGridViewRow row2 in this.dgvMain2.Rows)
                        {
                            if (!string.IsNullOrWhiteSpace(row2.Cells[0].Value as string))
                            {
                                if (!string.IsNullOrWhiteSpace(row2.Cells[1].Value as string))
                                {
                                    if (!View.ContainsKey(((string)row2.Cells[0].Value).Trim()))
                                    {
                                        View.Add(((string)row2.Cells[0].Value).Trim(), ((string)row2.Cells[1].Value).Trim());
                                    }
                                    else
                                    {
                                        MessageBoxHelper.ShowInfo("duplicate view rows!");
                                        return;
                                    }
                                }
                                else
                                {
                                    MessageBoxHelper.ShowInfo("sort is empty!");
                                    return;
                                }
                            }
                        }
                    }
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBoxHelper.ShowInfo("need schema rows!");
                    return;
                }
            }
            else
            {
                MessageBoxHelper.ShowInfo("need schema rows!");
                return;
            }
        }
    }
}
